const express = require('express');

const db = require('../db')

const md5 = require('md5-node')

const router = express.Router();

// --------------------------------------------------查看订单信息------------------------------------------
router.get('/get_order', (req, res) => {
    const { limit, page, state } = req.query;
    const { user_id } = req.user
    let sql;
    if (state != 0) {
        sql = `select * from order_list left outer join car_info on order_list.car_id=car_info.car_id  where user_id=${user_id} && state=${state} order by order_id desc`
    } else {
        sql = `select * from order_list left outer join car_info on order_list.car_id=car_info.car_id  where user_id=${user_id} && state in (1,2,3,4) order by order_id desc`
    }
    if (limit && page) {
        let start = ` limit ${(page - 1) * limit},${limit}`
        sql += start;
    }
    db(sql, (err, data) => {
        if (err) {
            res.send({
                code: -1,
                msg: err.sqlMessage,
            })
            throw err;
        }
        data.forEach((item, index) => {
            let order = {
                order_id: item.order_id,
                car_id: item.car_id,
                state: item.state,
                contract_id: item.contract_id,
                user_id: item.user_id,
                car_info: {}
            }
            delete item.order_id
            delete item.state
            delete item.contract_id
            delete item.user_id
            order.car_info = item;
            data[index] = order;
        });
        db('select count(1) as count from order_list', (err, reslut) => {
            if (err) {
                res.send({
                    code: -1,
                    msg: err.sqlMessage,
                })
                throw err;
            }
            res.send({
                code: 1,
                count: reslut[0].count,
                data
            })
        })
    })
})

// --------------------------------------------------新增订单-----------------------------------------------
router.post('/add_order', (req, res) => {
    const { car_id } = req.body;
    const { user_id } = req.user;
    db(`insert into order_list(car_id,state,user_id) values (${car_id},1,${user_id})`, (err, data) => {
        if (err) {
            res.send({
                code: -1,
                msg: err.sqlMessage,
            })
            throw err;
        }
        res.send({
            code: 1,
            msg: '添加成功！'
        })
    })
})

// --------------------------------------------------修改订单状态---------------------------------------------
router.post('/upd_order_state', (req, res) => {
    const { order_id, state } = req.body;
    db(`update order_list set state=${state} where order_id=${order_id}`, (err, data) => {
        if (err) {
            res.send({
                code: -1,
                msg: err.sqlMessage,
            })
            throw err;
        }
        res.send({
            code: 1,
            msg: '修改订单状态成功！'
        })

    })
})

// -------------------------------------------------签约-------------------------------------------------
router.post('/sign_contract', (req, res) => {
    const { name, phone, card_type, id_card, money, get_car_time, get_car_address, supply, order_id, code } = req.body;
    const { user_id } = req.user;
    db(`select count(1) as count from user where user_id=${user_id} && password ='${md5(code)}'`, (err, res1) => {
        if (err) {
            res.send({
                code: -1,
                msg: err.sqlMessage,
            })
            throw err
        }
        if (res1[0].count == 0) {
            res.send({
                code: 0,
                msg: '输入的支付密码错误!',
            })
        } else {
            const now = Date.parse(new Date()) / 1000
            db(`insert into contract(order_id,user_id,name,phone,card_type,id_card,money,get_car_time,get_car_address,supply,sign_time) values 
            (${order_id},${user_id},'${name}',${phone},${card_type},'${id_card}',${money},${get_car_time},'${get_car_address}','${supply}',${now})`, (err, res2) => {
                if (err) {
                    res.send({
                        code: -1,
                        msg: err.sqlMessage
                    })
                    throw err
                }
                db(`update order_list set state=2,contract_id=${res2.insertId} where order_id=${order_id}`, (err, data) => {
                    if (err) {
                        res.send({
                            code: -1,
                            msg: err.sqlMessage,
                        })
                        throw err;
                    }
                    res.send({
                        code: 1,
                        msg: '签约成功！'
                    })
                })
            })
        }
    })

})

// ------------------------------------------------------查看签约信息-------------------------------------------------
router.get('/get_contract', (req, res) => {
    const { contract_id } = req.body;
    let sql;
    sql = `select * from contract where contract_id = ${contract_id}`
    db(sql, (err, data) => {
        if (err) {
            res.send({
                code: -1,
                msg: err.sqlMessage,
            })
            throw err;
        }
        res.send({
            code: 1,
            data
        })
    })

})

module.exports = router